<?php
class diy_uicompentControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		$where="  status=2";
		$url="/admin.php?m=diy_uicompent";
		$option=array(
			"where"=>$where
		);
		$rscount=true;
		$data=M("diy_uicompent")->select($option,$rscount);
		 
		$this->smarty->goAssign(array(
			"data"=>$data,
			 
		));
		$this->smarty->display("diy_uicompent/index.html");
	}
	
	public function onAdd(){
		$uiList=M("uicompent")->select(array(
			"where"=>" status=2 ",
			"fields"=>" id,title,description,imgurl"
		));
		$this->smarty->goAssign(array(
			"uiList"=>$uiList,
			
		));
		$this->smarty->display("diy_uicompent/add.html");
	}
	public function onSave(){
		 
		$data['ucid']=$ucid=get_post("ucid","i");
		$uc=M("uicompent")->selectRow("id=".$ucid);
		if(empty($uc)){
			$this->goAll("请选择组件",1);
		} 
		 
		$data=M("diy_uicompent")->postData();
		//检查tag
		$row=M("diy_uicompent")->selectRow(" tag='".$data['tag']."'");
		if($row){
			$this->goAll("标签已经存在请更换",1);
		}
		 
		$data['jsondata']=$uc['jsondata'];
		$data['typeid']=$uc['typeid'];
		$data['shopid']=SHOPID;
		extract(json_decode(base64_decode($uc['jsondata']),true));
		$tpl=$uc['content'];		 
		$tpl=str_replace('"','\"',$tpl);
		eval("\$content = \"$tpl\";");
		$data['content']=$content;
		M("diy_uicompent")->insert($data);
		cache()->set("upg".SHOPID."-".$data['tag'],0,-10);
		$this->goAll("保存成功");
	}
	public function onEdit(){
		$id=get("id",'i');
		$data=array();
		$data=M("diy_uicompent")->selectRow("id=".$id);
		$uc=M("uicompent")->selectRow("id=".$data['ucid']);
		$jsondata=json_decode(base64_decode($data['jsondata']),true);
		$funlist=$this->getFunList($uc['funtype']);
		 
		$this->smarty->goAssign(array(
			"data"=>$data,
			"funlist"=>$funlist,
			"jsondata"=>$jsondata,
			 
		));
		
		if($uc['typeid']==2){
			$this->smarty->display("diy_uicompent/edit_md.html");
		}elseif($uc['typeid']==3){
			$this->smarty->display("diy_uicompent/edit_fullpage.html");
		}else{
			$this->smarty->display("diy_uicompent/edit.html");
		}
		
	}
	public function getFunList($type){
		switch($type){
			case "pro-list":
				return array(
					"pro-new"=>"产品-新品",
					"pro-hot"=>"产品-热门",
					"pro-recommend"=>"产品-推荐",
				);
				break;
			case "art-list":
				return array(
					"art-new"=>"文章-新品",
					"art-hot"=>"文章-热门",
					"art-recommend"=>"文章-推荐",
				);
				break;
			case "pic-list":
				return array(
					"pic-new"=>"图片-新品",
					"pic-hot"=>"图片-热门",
					"pic-recommend"=>"图片-推荐",
				);				
				break;
			case "tags":
				return array(
					"tags-article"=>"文章标签",
					"tags-product"=>"产品标签",
					"tags-picture"=>"图片标签"
				);
				break;
			case "nav-wap":
				return array(
					"nav-wap"=>"wap导航"
				);
				break;
			default:
				return array(
					"default"=>"默认"
				);
				break;
		}
		 
		 
	}
	public function oneditSave(){
		$id=get_post("id","i");
		$data['ucid']=$ucid=get_post("ucid","i");
		$uc=M("uicompent")->selectRow("id=".$ucid);
		$content="";
		$tag=post("tag","h");
		if($uc['typeid']==1){
			extract($_POST['jsondata']);
			ob_start();
			require $uc['tpl'];
			$content=ob_get_contents();
			ob_end_clean();
			 

		} 
		$data=M("diy_uicompent")->postData();
		$jsondata=json_decode(base64_decode($uc['jsondata']),true);
		foreach($jsondata as $k=>$v){
			if(isset($_POST['jsondata'][$k])){
				$jsondata[$k]=$_POST['jsondata'][$k];
			}
		}
		$data['jsondata']=base64_encode(json_encode($jsondata));
		$data['content']=$content;
		 
		if($id){
			M("diy_uicompent")->update($data,"id=".$id);
		}else{
			M("diy_uicompent")->insert($data);
		}
		cache()->set("upg-".$data['tag'],0,-10);
		$this->goAll("保存成功");
	}
	
	public function onView(){
		echo '
		<link href="/plugin/mui/css/mui.min.css" rel="stylesheet">
		<link href="/plugin/animate/animate.css" rel="stylesheet">
		<link href="//at.alicdn.com/t/font_c19suxic1qestt9.css" rel="stylesheet" />
		<link href="/plugin/weizhan/weizhan.css?<?php echo time();?>" rel="stylesheet" />
		<link href="/plugin/uicompent/uicompent.css?{$smarty.now}" rel="stylesheet"  />
		<script src="/plugin/jquery/jquery-3.1.1.min.js"></script>
		<script src="/plugin/mui/js/mui.min.js"></script>
		<script src="/plugin/weizhan/weizhan.js"></script>
		';
		$id=get("id");
		M("diy_uicompent")->view($id);
	}
	
	public function onViewData(){
		
		$data['ucid']=$ucid=get_post("ucid","i");
		$uc=M("uicompent")->selectRow("id=".$ucid);
		$config=$_POST['jsondata'];
		if($uc['typeid']==1){
			extract($_POST['jsondata']);
			require $uc['tpl'];
		}elseif($uc['typeid']==2){
			 
			echo M("diy_uicompent")->getData($uc['funtype'],$config,$uc['tpl']);	
		}
		
		
	}
	public function onDelete(){
		$id=get("id","i");
		M("diy_uicompent")->delete("id=".$id);
		$this->goAll("删除成功");
	}
	
}
?>